package com.swensen.game.editor;

import javax.swing.JFrame;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JToolBar;

import java.awt.Component;
import java.awt.Panel;
import java.awt.ScrollPane;

import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JSpinner;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import javax.swing.Box;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import com.swensen.game.Board;
import com.swensen.game.data.BoardData.LevelType;
import com.swensen.game.data.GameState;
import com.swensen.game.data.TileSet;
import com.swensen.game.editor.data.EditorBoardData;
import com.swensen.game.editor.event.EditorCycleEvent;
import com.swensen.game.editor.event.EditorCycleEventListener;

import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;

/**
 * The Class EditorFrame.
 */
public class EditorFrame extends JFrame implements ChangeListener {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = -986426777478440255L;

	/**
	 * The main method.
	 *
	 * @param args the arguments
	 */
	public static void main(String[] args) {
		new EditorFrame();
	}
	
	private EditorBoard board;
	
	/**
	 * Instantiates a new editor frame.
	 */
	public EditorFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(800, 600);
        setLocationRelativeTo(null);
        setTitle("Dumboy Editor");
        setResizable(true);
        getContentPane().setLayout(new BorderLayout(0, 0));

        board = new EditorBoard();

        GameState.loadGameState(new EditorBoardData("resources" + File.separatorChar + "Tiles"));
        
                
        setJMenuBar(buildMenuBar());
        
        getContentPane().add(this.buildNorthToolBar(), BorderLayout.NORTH);
        
        Panel southPanel = new Panel();
        getContentPane().add(southPanel, BorderLayout.SOUTH);
        
        Panel eastPanel = new Panel();
        getContentPane().add(eastPanel, BorderLayout.EAST);
        
        ScrollPane center = new ScrollPane();
        getContentPane().add(center, BorderLayout.CENTER);
        setVisible(true);
        center.add(board);
        
        JPanel panel = new JPanel();
        panel.setBackground(SystemColor.window);
        getContentPane().add(panel, BorderLayout.WEST);
        
        Box verticalBox = Box.createVerticalBox();
        panel.add(verticalBox);

        String[] options = {"Tile Set", "Sprite Set"};
        
        JComboBox<String> comboBox = new JComboBox(options);
        verticalBox.add(comboBox);
        comboBox.setSelectedIndex(0);
        comboBox.setEditable(false);
        
        JToolBar resourceToolBar = new JToolBar();
        resourceToolBar.setAlignmentX(Component.RIGHT_ALIGNMENT);
        resourceToolBar.setFloatable(false);
        verticalBox.add(resourceToolBar);
                
        JButton btnImportTiles = new JButton("+");
        resourceToolBar.add(btnImportTiles);
        btnImportTiles.setToolTipText("Import Tiles");
        
        JButton btnDeleteTiles = new JButton("-");
        btnDeleteTiles.setToolTipText("Delete Tiles");
        resourceToolBar.add(btnDeleteTiles);
        
        final TileSelector selector = new TileSelector();
        verticalBox.add(selector);
        
        board.addEditoryCycleEventListener(new EditorCycleEventListener() {

			@Override
			public void cycleDispatched(EditorCycleEvent e) {
				selector.repaint();
			}
        	
        });
        board.requestFocus();
	}
	
	/**
	 * Builds the menu bar.
	 *
	 * @return the j menu bar
	 */
	private JMenuBar buildMenuBar() {
		JMenuBar mainMenuBar = new JMenuBar();
		mainMenuBar.setBackground(SystemColor.window);
        
        JMenu mnFile = new JMenu("File");
        mainMenuBar.add(mnFile);
        
        JMenuItem mntmSave = new JMenuItem("Save");
        mnFile.add(mntmSave);
        
        JMenuItem mntmSaveas = new JMenuItem("SaveAs");
        mnFile.add(mntmSaveas);
        
        JMenuItem mntmOpen = new JMenuItem("Open");
        mnFile.add(mntmOpen);
        
        JMenu mnEdit = new JMenu("Edit");
        mainMenuBar.add(mnEdit);
        
        JMenuItem mntmUndo = new JMenuItem("Undo");
        mnEdit.add(mntmUndo);
        
        JMenuItem mntmRedo = new JMenuItem("Redo");
        mnEdit.add(mntmRedo);
        
        return mainMenuBar;
	}
	
	public JToolBar buildNorthToolBar() {
		JToolBar toolBar = new JToolBar();
		
		JComboBox<LevelType> comboBox = new JComboBox<LevelType>(LevelType.values());
		toolBar.add(comboBox);
        comboBox.setSelectedIndex(0);
        comboBox.setEditable(false);
        
        comboBox.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JComboBox<LevelType> item = (JComboBox<LevelType>)e.getSource();
				LevelType level = (LevelType)item.getSelectedItem();
				
				GameState.INSTANCE.<EditorBoardData>getBoardData().setLevel(level.getValue());
				board.requestFocus();
			}
        	
        });
        
        
		return toolBar;
	}

	/* (non-Javadoc)
	 * @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent)
	 */
	@Override
	public void stateChanged(ChangeEvent e) {
		if (e.getSource() instanceof JSpinner) {
			JSpinner spinner = (JSpinner)e.getSource();
			SpinnerModel spinnerModel = spinner.getModel();
			if (spinnerModel instanceof SpinnerNumberModel) {
				
			}
		}
		
	}
}
